package cn.itcast.user.controller;

import cn.itcast.hmall.utils.ThreadLocalUtil;
import cn.itcast.hmall.pojo.user.Address;
import cn.itcast.hmall.pojo.user.User;

import cn.itcast.user.mapper.UserMapper;
import cn.itcast.user.service.AddressService;
import cn.itcast.user.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;


import java.util.List;

@RestController
@RequestMapping("/address")
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private AddressService addressService;

    @GetMapping("/uid")
    public List<Address> findAddressByUid() {
 //       String uid ="2";
        Long userId = ThreadLocalUtil.getUserId();
        String uid = ThreadLocalUtil.getUserId().toString();
        System.out.println(uid);
        List<Address> list = this.addressService.seleceByUid(uid);
        return list;
    }

    @GetMapping("/{addressId}")
    public Address add(@PathVariable String addressId) {
        System.out.println(addressId);
        Address byId = addressService.selById(addressId);
        System.out.println(byId);
        return byId;
    }
    @GetMapping("/get/{uid}")
    public User finduser(@PathVariable Long uid){
//        System.out.println(uid);
        User byId = userMapper.selectById(uid);
        System.err.println(byId);
        return byId;
    }
    @PutMapping
    public boolean updateUser(@RequestBody User user){
        System.err.println(user);
        LambdaQueryWrapper<User> wrapper = Wrappers.<User>lambdaQuery();
        wrapper.eq(User::getId, user.getId());

        int update = userMapper.update(user, wrapper);
        return update==0? false:true;
    }
}
